package com.gitee.feizns.itrip.personal.trade.controller;

import com.gitee.feizns.itrip.personal.beans.pojo.User;
import com.gitee.feizns.itrip.personal.utils.component.annotations.CurtUser;
import com.gitee.feizns.itrip.personal.utils.service.AliPayService;
import com.gitee.feizns.itrip.personal.trade.entity.vo.ForViewConfirmOrder;
import com.gitee.feizns.itrip.personal.trade.service.TradeHotelOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.gitee.feizns.itrip.personal.trade.service.TradeHotelOrderService.ALI_PAY_TYPE;

/**
 * @author feizns
 * @since 2019/1/13 0013
 */
@RestController
@RequestMapping("/api")
public class AliPaymentController {

    /**
     * 订单完成
     */
    public static final String TRADE_FINISHED = "TRADE_FINISHED";

    /**
     * 付款成功
     */
    public static final String TRADE_SUCCESS = "TRADE_SUCCESS";

    /**
     * 确认订单成功
     */
    public static final String SUCCESS = "success";

    /**
     * 确认订单失败
     */
    public static final String FAIL = "fail";

    @Autowired
    private AliPayService aliPayService;

    @Autowired
    private TradeHotelOrderService tradeHotelOrderService;

    @GetMapping(value = "/prepay/{orderNo}", produces = "text/html;charset=utf-8")
    public String prepay(@PathVariable String orderNo, @CurtUser User user) {
        ForViewConfirmOrder order = tradeHotelOrderService.toPayOrder(orderNo);
        return aliPayService.send(order.getOrderNo(),
                String.valueOf(order.getPayAmount()),
                order.getHotelName(),
                null);
    }

    @PostMapping("/notifyUrl")
    public void notify(@RequestParam("out_trade_no") String outTradeNo,
                       @RequestParam("trade_no") String tradeNo,
                       @RequestParam("trade_status") String tradeStatus,
                       @RequestParam("total_amount") String totalAmount,
                       HttpServletRequest req,
                       HttpServletResponse resp) throws IOException {

        boolean signVerified = aliPayService.rsaCheckV1(req);

        if ( signVerified ) {
            if( TRADE_SUCCESS.equals(tradeStatus)
                    || TRADE_FINISHED.equals(tradeStatus) ) {
                tradeHotelOrderService.confirmOrder(outTradeNo, tradeNo, ALI_PAY_TYPE);
            }
        }

        resp.getWriter().println(signVerified ? SUCCESS : FAIL);
    }

    @GetMapping("/returnUrl")
    public void returnUrl(@RequestParam("out_trade_no") String outTradeNo,
                            HttpServletRequest req,
                            HttpServletResponse resp) throws IOException {

        Long id = tradeHotelOrderService.getOrderIdByOrderNo(outTradeNo);

        if ( aliPayService.rsaCheckV1(req) )
            resp.sendRedirect("http://localhost/index.html#/orderpaystate?orderNo=" + outTradeNo + "&id=" + id + "&_k=u0rd05");
    }

}
